function Sb_Form_Validation( $form,formParams ){
	this.$_form = $form;
	this.formParams = formParams;
	this.elements = [ ];
	this.errorElements = [ ];
	this.isValidElement = {};
	this.isValidForm = true;
	this.errorClass = 'ui-state-error';
	this.init( );
}

Sb_Form_Validation.prototype = {
	
	getElm : function( id ){
		if('undefined' == typeof this.elements[ id ]){
			this.elements[ id ] = $( '#'+id );
		}
		return this.elements[ id ] ;
	},
	
	resetErrors : function(){
		this.isValidForm = true;
		for(var k in this.elements){
			this.elements[ k ].removeClass( this.errorClass );
		}
		for(var j in this.errorElements){
			this.errorElements[ j ].remove( );
		}
		this.errorElements = [ ];
		return this;
	},
	
	appendErrorMsg : function( id,msg ){
		var ulId = 'ul'+id;
		var errorDetails = '';
		if('undefined' == typeof this.errorElements[ ulId ]){
			errorDetails = '<ul class="'+this.errorClass+'" id="'+ulId+'" ">';
		}
		for(var i = 0, len = msg.length; i < len; i++){
			errorDetails += '<li>'+msg[ i ]+'</li>';
		}
		if('undefined' == typeof this.errorElements[ ulId ]){
			errorDetails += '</ul>';
			this.elements[ id ].after( errorDetails );
			this.errorElements[ ulId ] = $( '#'+ ulId );
		}else{
			this.errorElements[ ulId ].append( errorDetails );
		}
		return this;
	},
	
	initCleanError : function(){
		var _localObj = this;
		for(var k in this.elements){
			(function( i ){
				_localObj.elements[ i ].focus(function(){
					_localObj.elements[ i ].removeClass( _localObj.errorClass );
					var ulErrorId = 'ul'+ _localObj.elements[ i ].attr( 'id' ) ;
					if('undefined' != typeof _localObj.errorElements[ ulErrorId ]){
						_localObj.errorElements[ ulErrorId ].remove( );
					}
				});
			})( k )
		}
		return this;
	},
	
	isValid : function( ){
		this.isValidElement = {};
		this.resetErrors( );
		var classMethods = window["Sb_Form_Validation"]["prototype"];
		var params = [ ];
		for(var i = 0, len = this.formParams.length; i < len; i++){
			params = [ this.formParams[i].id,this.formParams[i].msg];
			for(var k = 0,kLen = this.formParams[i].validators.length; k < kLen; k++){
				this.isValidElement[this.formParams[i].id] = this.isValidElement[this.formParams[i].id] || {};
				if(this.formParams[i].validators[ k ].params.length){
					params = params.concat( this.formParams[i].validators[ k ].params);
				}
				classMethods[this.formParams[i].validators[ k ].name].apply(this,params );
				if(!this.isValidElement[this.formParams[i].id].isValid){
					break;
				}
					
			}
		}
		return this.initCleanError( ).isValidForm;
	},
	
	required : function(id,msg ){
		var $_elm = this.getElm( id );
		this.isValidElement[id].isValid = true;
		if('' == $_elm.val()){
			$_elm.addClass( this.errorClass );
			this.isValidForm = false;
			this.appendErrorMsg( id,msg );	
			this.isValidElement[id].isValid = false;
		}
		return this;	
	},
	
	alnum : function( ){
		//alert( 'alnum' );
		return this;	
	},
	
	stringLength : function( id,msg,min,max){
		this.isValidElement[id].isValid = true;
		var isValid = true;
		var $_elm = this.getElm( id );
		var val = $_elm.val( ).length;
		if('undefined' != typeof min){
			if(val < min){
				isValid = false;
			}
		}
		if('undefined' != typeof max){
			if(val > max){
				isValid = false;
			}
		}
		if(!isValid){
			this.isValidElement[id].isValid = false;
			$_elm.addClass( this.errorClass );
			this.appendErrorMsg( id,msg );	
		}
		return this;	
	},
	
	regex : function( ){
		//alert( 'regex' );
		return this;	
	},
	
	init : function(){
		var _localObj = this;
		this.$_form.submit(function(){
			return _localObj.isValid( );
		});
		return this;
	}
};